package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.physicaleducationmajor.PhysicalEducationMajorAddDto;
import com.teacher.pojo.dto.physicaleducationmajor.PhysicalEducationMajorGetDto;
import com.teacher.pojo.dto.physicaleducationmajor.PhysicalEducationMajorUpdateDto;
import com.teacher.pojo.entity.PhysicalEducationMajor;
import com.teacher.pojo.vo.physicaleducationmajor.PhysicalEducationMajorGetVo;
import com.teacher.service.IPhysicalEducationMajorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 体育专业类体育成绩 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/pc/physical-education-major")
@Api(tags = "体育专业类体育成绩")
public class PhysicalEducationMajorController {
    @Autowired
    private IPhysicalEducationMajorService iPhysicalEducationMajorService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 体育专业类体育成绩
     *
     * @param dto
     * @return
     */
    @ApiOperation("体育专业类体育成绩 physicalEducationMajor_add")
    @PostMapping
    @PreAuthorize("hasAuthority('physicalEducationMajor_add')")
    public CommonResult<String> add(@RequestBody @Validated PhysicalEducationMajorAddDto dto) {
        PhysicalEducationMajor physicalEducationMajor = new PhysicalEducationMajor();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, physicalEducationMajor);
        if (iPhysicalEducationMajorService.save(physicalEducationMajor)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新非体育类专业体育课成绩
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新非体育类专业体育课成绩 physicalEducationMajor_update")
    @PutMapping
    @PreAuthorize("hasAuthority('physicalEducationMajor_update')")
    public CommonResult<String> update(@RequestBody @Validated PhysicalEducationMajorUpdateDto dto) {
        PhysicalEducationMajor physicalEducationMajor = new PhysicalEducationMajor();
        BeanUtils.copyProperties(dto, physicalEducationMajor);
        if (iPhysicalEducationMajorService.updateById(physicalEducationMajor)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除非体育类专业体育课成绩
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除非体育类专业体育课成绩 physicalEducationMajor_delete")
    @PreAuthorize("hasAuthority('physicalEducationMajor_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iPhysicalEducationMajorService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询非体育类专业体育课成绩
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询非体育类专业体育课成绩 physicalEducationMajor_get")
    @PreAuthorize("hasAuthority('physicalEducationMajor_get')")
    public CommonResult<IPage<PhysicalEducationMajorGetVo>> getPage(PhysicalEducationMajorGetDto dto) {
        return iPhysicalEducationMajorService.getPage(dto);
    }

    /**
     * 导入非体育类专业体育课成绩
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入非体育类专业体育课成绩 physicalEducationMajor_upload")
    @PreAuthorize("hasAuthority('physicalEducationMajor_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iPhysicalEducationMajorService.upload(file);
    }
}

